# -*- coding:utf8 -*-
from __future__ import print_function
import datetime
import calendar


def to_yyyymmdd(date_time):
    """
    将时间转换为字符串
    """
    return date_time.strftime('%Y%m%d')


def to_yyyymmddhhmmss(date_time):
    """
    将时间转换为字符串
    """
    return date_time.strftime('%Y%m%d%H%M%S')


def to_yyyymmdd(date_time):
    """
    将时间转换为字符串
    """
    return date_time.strftime('%Y%m%d')


def to_yyyy_mm_dd(date_time=None):
    if date_time is None:
        date_time = datetime.datetime.now()

    return date_time.strftime('%Y-%m-%d')


def to_hhommoss(time_obj):
    """
    获取时间节点
    :param time_obj: 
    :return: 
    """
    return time_obj.strftime('%H:%M:%S')


def from_yyyymmdd(str_):
    """

    :param str_:
    :return:
    """
    time = datetime.datetime.strptime(str_, '%Y%m%d')
    return time


def from_yyyy_mm_dd_hh_mm_ss(str_):
    """
    从字符串到时间格式
    :param str_:
    :return:
    """
    time = datetime.datetime.strptime(str_, '%Y-%m-%d %H:%M:%S')
    return time


def to_yyyy_mm_dd_hh_mm_ss(date_time):
    """
    从字符串到时间格式
    :param str_:
    :return:
    """
    return date_time.strftime('%Y-%m-%d %H:%M:%S')


def to_yyyy_mm_dd_hh_mm(date_time):
    """
    从字符串到时间格式
    :param str_:
    :return:
    """
    return date_time.strftime('%Y-%m-%d %H:%M')


def json_if_dateime(date_time, format='%Y%m%d%H%M%S'):
    """
    如果是时间time格式则进行json话
    """
    if isinstance(date_time, (datetime.datetime, datetime.date, datetime.time)):
        return date_time.strftime(format)
    return date_time


def daytime_range_from_yyyymm(yyyymm):
    """
    获取时间区间
    :param yyyymm:
    :return:
    """
    year = int(yyyymm[:4])
    month = int(yyyymm[-2:])

    # 获取当月第一天的星期和当月的总天数
    firstDayWeekDay, monthRange = calendar.monthrange(year, month)

    # 获取当月的第一天
    firstDay = datetime.date(year=year, month=month, day=1)
    lastDay = datetime.date(year=year, month=month, day=monthRange)
    nextDay = lastDay + datetime.timedelta(days=1)

    return firstDay, lastDay, nextDay



if __name__ == '__main__':
    print(to_yyyymmddhhmmss(datetime.datetime.now()))
    t = datetime.time(hour=8)
    print(to_hhommoss(t))
    print(daytime_range_from_yyyymm('201801'))